package bingo.component.amchart.service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import net.bingosoft.common.BeanHelper;
import net.bingosoft.common.ObjectRegistry;
import net.bingosoft.common.data.BaseDao;
import net.bingosoft.common.data.SQLProvider;
import net.bingosoft.common.data.SQLProvider.DynamicSQL;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import bingo.component.amchart.graphics.Graphic;
import bingo.component.amchart.graphics.Graphic.GraphicType;
import bingo.component.amchart.graphics.chart.Chart;
import bingo.component.amchart.graphics.chart.Graph;
import bingo.component.amchart.graphics.chart.Graphs;
import bingo.component.amchart.graphics.chart.Guide;
import bingo.component.amchart.graphics.chart.Guides;
import bingo.component.amchart.graphics.chart.Series;
import bingo.component.amchart.graphics.chart.Value;
import bingo.component.amchart.graphics.pie.Pie;
import bingo.component.amchart.graphics.pie.Slice;

/**
 * Jun 24, 2008 5:09:54 PM
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:
 * ChangeLog:
 */
public class GraphicReaderService implements IGraphicReaderService{
	private BaseDao dao = ObjectRegistry.getBaseDao();
	
	
	public Graphic getGraphic(String sqlId,GraphicType type,Map<String,Object> params) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		DynamicSQL realSQL = null;
		try {
			realSQL = SQLProvider.getDynamicSQL(sqlId,params);
		} catch (NoSuchMethodException e1) {
			throw new RuntimeException(e1);
		}
		if(type == GraphicType.pie){
			Pie pie = new Pie();
			List<Slice> sliceList = dao.list(Slice.class, realSQL.getSql(),realSQL.getParams());
			pie.setSliceList(sliceList);
			return pie;
		}else if(type ==GraphicType.bar || type == GraphicType.line){
			Assert.notNull(params.get("graphCount"), "graphCount can not be null ...");
			int graphCount = Integer.valueOf(params.get("graphCount").toString());
			Chart chart = new Chart();
			List<Map<String, Object>> resultList = dao.list(realSQL.getSql(),realSQL.getParams());
			Series series = new Series();
			Graphs graphs = new Graphs();
			
			Guides guides = new Guides();
			
			for(Map <String,Object> result : resultList){
				//设置系列
				series.addValue(BeanHelper.Map2ObjectIgnoreCase(result, new Value(), "SERIES_"));
				for(int i = 1 ;i <= graphCount;i++){
					Graph graph = new Graph();
					//Object gxid = result.get("GRAPH"+i+"_XID"); required by lugang
					Object gxid = result.get("SERIES"+i+"_XID");
					if(gxid == null){
						gxid = i;
					}
					
					graph.setGid(gxid.toString());
					
					//Object gtitle = result.get("GRAPH"+i+"_TITLE"); required by lugang
					Object gtitle = result.get("SERIES"+i+"_TITLE");
					if(gtitle == null){
						gtitle = "GRAPH_"+i ;
					}
					graph.setTitle(gtitle.toString());
					
					graphs.addValueToGraph(i-1,BeanHelper.Map2ObjectIgnoreCase(result, new Value(), "GRAPH"+i+"_"),graph);
					
				}
				
			}
			
			String guideCount = (String)params.get("guideCount");
			
			if (StringUtils.isNotEmpty(guideCount)&&StringUtils.isNumeric(guideCount)) {
				if(resultList.size() > 0){
					for(int i = 1; i <= Integer.parseInt(guideCount); i ++){
						guides.addGuide(BeanHelper.Map2ObjectIgnoreCase(resultList.get(0), new Guide(), "GUIDE" + i + "_"));
					}
				}
			}
			
			chart.setSeries(series);
			chart.setGraphs(graphs);
			chart.setGuides(guides);
			return chart;
		}
		return null;
	}
}
